/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accountproject;

import accountproject.data.DataIO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;

/**
 *
 * @author Amon
 */
class CustomerListModel extends DefaultListModel {

    private DataIO dataIO;
    private HashMap<String, Integer> customerMap;

    public CustomerListModel() {

        super();

        readCustomers();
    }

    private void readCustomers() {
        try {
            if (null == dataIO) {
                dataIO = new DataIO();
            }
            removeAllElements();
            customerMap = new HashMap<String, Integer>();

            String listElement;
            ResultSet rs = dataIO.getCustomers();
            while (rs.next()) {
                listElement = rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(5);
                addElement(listElement);
                customerMap.put(listElement, rs.getInt(1));
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerListModel.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public int getCustomerIndex(String customerName) {
        return customerMap.get(customerName);
    }
}
